home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 165 / applic / notepad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-19  |  12.6 KB  |  560 lines

  1. /***                The Pad        version 1.0
  2.  *             Notepad desk accessory
  3.  *             by BILL FOSTER 6/03/87
  4.  *
  5.  *    This program has been placed in the public domain and may
  6.  *    be distibuted freely.  Any improvements to this code, or
  7.  *    donations please send info to
  8.  *
  9.  *        attn: Bill Foster
  10.  *          Infolab Inc.
  11.  *         23240 Lori Way
  12.  *        Hayward, CA 94541
  13.  *        ph (415) 581-0739
  14.  *
  15.  *    Modify to run on COLOR systems
  16. */
  17. /*********************************************************************/
  18. /*    Portions of this code Copyright C1985 by Atari Corp.         */
  19. /* INCLUDE FILES                             */
  20. /*********************************************************************/
  21.  
  22. #include <stdio.h.>
  23. #include <obdefs.h>
  24. #include <gemdefs.h>
  25. #include <osbind.h>
  26. #include <ctype.h>
  27.  
  28. /*********************************************************************/
  29. /* DEFINES                                    */
  30. /*********************************************************************/
  31.  
  32. #define TRUE 1
  33. #define FALSE 0
  34. /* #define WI_KIND        (SIZER|MOVER|FULLER|CLOSER|NAME) */
  35. #define WI_KIND        (MOVER|CLOSER|NAME)
  36.  
  37. #define NO_WINDOW (-1)
  38.  
  39. #define MIN_WIDTH  (2*gl_wbox)
  40. #define MIN_HEIGHT (3*gl_hbox)
  41.  
  42. /*********************************************************************/
  43. /* EXTERNALS                                    */
  44. /*********************************************************************/
  45.  
  46. extern int    gl_apid;
  47.  
  48. /*********************************************************************/
  49. /* GLOBAL VARIABLES                                */
  50. /*********************************************************************/
  51.  
  52. int    gl_hchar;
  53. int    gl_wchar;
  54. int    gl_wbox;
  55. int    gl_hbox;    /* system sizes */
  56.  
  57. int    menu_id ;    /* our menu id */
  58.  
  59. int     phys_handle;    /* physical workstation handle */
  60. int     handle;        /* virtual workstation handle */
  61. int    wi_handle;    /* window handle */
  62. int    top_window;    /* handle of topped window */
  63.  
  64. int    xdesk,ydesk,hdesk,wdesk;
  65. int    xold,yold,hold,wold;
  66. int    xwork,ywork,hwork,wwork;    /* desktop and work areas */
  67.  
  68. int    msgbuff[8];    /* event message buffer */
  69. int    keycode;    /* keycode returned by event-keyboard */
  70. int    mx,my;        /* mouse x and y pos. */
  71. int    but;        /* for event_multi    */
  72. int    ret;        /* dummy return variable */
  73.  
  74. int    hidden;        /* current state of cursor */
  75.  
  76. int    contrl[12];
  77. int    intin[128];
  78. int    ptsin[128];
  79. int    intout[128];
  80. int    ptsout[128];    /* storage wasted for idiotic bindings */
  81.  
  82. int work_in[11];    /* Input to GSX parameter array */
  83. int work_out[57];    /* Output from GSX parameter array */
  84. int pxyarray[10];    /* input point array */
  85.  
  86. /****************************************************************/
  87. /*  GSX UTILITY ROUTINES.                    */
  88. /****************************************************************/
  89.  
  90. hide_mouse()
  91. {
  92.     if(! hidden){
  93.         graf_mouse(M_OFF,0x0L);
  94.         hidden=TRUE;
  95.     }
  96. }
  97.  
  98. show_mouse()
  99. {
  100.     if(hidden){
  101.         graf_mouse(M_ON,0x0L);
  102.         hidden=FALSE;
  103.     }
  104. }
  105.  
  106. /****************************************************************/
  107. /* open virtual workstation                    */
  108. /****************************************************************/
  109. open_vwork()
  110. {
  111. register int i;
  112.  
  113.     for (i=0; i<10; work_in[i++] = 1);
  114.     work_in[10] = 2;
  115.     handle = phys_handle;
  116.     v_opnvwk (work_in, &handle, work_out);
  117. }
  118.  
  119. /****************************************************************/
  120. /* set clipping rectangle                    */
  121. /****************************************************************/
  122. set_clip (x, y, w, h)
  123. register int x, y;
  124. int w, h;
  125. {
  126. int clip[4];
  127.     *clip = x;
  128.     clip[1] = y;
  129.     clip[2] = x+w;
  130.     clip[3] = y+h;
  131.     vs_clip (handle, 1, clip);
  132. }
  133.  
  134. /****************************************************************/
  135. /* open window                            */
  136. /****************************************************************/
  137. open_window()
  138. {
  139.     wi_handle = wind_create (WI_KIND, xdesk, ydesk, wdesk, hdesk);
  140.     graf_growbox (xdesk+wdesk/2,ydesk+hdesk/2,gl_wbox,gl_hbox,xdesk,ydesk,wdesk,hdesk);
  141.     wind_open (wi_handle, xdesk, ydesk, wdesk, hdesk);
  142.     wind_get (wi_handle, WF_WORKXYWH, &xwork, &ywork, &wwork, &hwork);
  143. }
  144.  
  145. /****************************************************************/
  146. /* find and redraw all clipping rectangles            */
  147. /****************************************************************/
  148.  
  149. do_redraw (xc, yc, wc, hc)
  150. int xc, yc, wc, hc;
  151. {
  152.     GRECT t1, t2;
  153.  
  154.     hide_mouse ();
  155.     wind_update (TRUE);
  156.     t2.g_x = xc;
  157.     t2.g_y = yc;
  158.     t2.g_w = wc;
  159.     t2.g_h = hc;
  160.     wind_get (wi_handle, WF_FIRSTXYWH, &t1.g_x, &t1.g_y, &t1.g_w, &t1.g_h);
  161.     while (t1.g_w && t1.g_h) {
  162.       if (rc_intersect (&t2, &t1)) {
  163.         set_clip (t1.g_x, t1.g_y, t1.g_w, t1.g_h);
  164.         draw_sample ();
  165.       }
  166.       wind_get (wi_handle, WF_NEXTXYWH, &t1.g_x, &t1.g_y, &t1.g_w, &t1.g_h);
  167.     }
  168.     wind_update(FALSE);
  169.     show_mouse();
  170. }
  171.  
  172. /****************************************************************/
  173. /*        Accessory Unit -  Until First Event_Multi    */
  174. /****************************************************************/
  175.  
  176. #define    PAD_FILE    "notepad.sys"
  177. #define    PAGES        9
  178. #define    PAGE_SIZE    (40*11)
  179. #define    COLUMNS        40        /* Character width & height */
  180. #define    LINES        11
  181.  
  182. int    ccol, cline;            /* cursor column and row */
  183. int    scol[PAGES], sline[PAGES];    /* Save cursor position */
  184. int    page;                /* Current page number */
  185. int    cell_width;
  186. int    cell_height;            /* Height of character in pixels */
  187. char    *pad_ram;
  188. char    file[20];
  189. char    pad[100];
  190.  
  191. main ()
  192. {
  193.     int            attrib[10];
  194.     register int    fd, c;
  195.     register long    l;
  196.     register char    *p;
  197.     extern char        *calloc ();
  198.  
  199.     if ((pad_ram = calloc (PAGES, PAGE_SIZE)) != NULL)
  200.     {
  201.     appl_init ();
  202.     phys_handle = graf_handle (&gl_wchar, &gl_hchar, &gl_wbox, &gl_hbox);
  203.     menu_id = menu_register (gl_apid, "  Notepad");
  204.     wind_get (0, WF_WORKXYWH, &xdesk, &ydesk, &wdesk, &hdesk);
  205.     c = Getrez ();
  206.     if (c == 2)
  207.         cell_height = 16;
  208.     else
  209.         cell_height = 8;
  210.     cell_width = 8;
  211.     hdesk /= 2;            /* Half screen        */
  212.     wdesk /= 2;            /* Quarter Screen    */
  213.     hdesk += cell_height / 2;
  214.     xdesk += wdesk/2;
  215.     wdesk += 2;
  216.     ydesk += 25;
  217.     wi_handle = NO_WINDOW;
  218.     hidden = FALSE;
  219.     page = ccol = cline = 0;
  220.     p = pad_ram;
  221.     for (c=0; c < (PAGE_SIZE * PAGES); c++)        /* Clear workspace */
  222.         *p++ = ' ';
  223.     *file = 'A' + Dgetdrv ();
  224.     file[1] = ':';
  225.     strcpy (file+2, PAD_FILE);
  226.     fd = Fopen (file, 0);    
  227.     if (fd >= 0)
  228.     {
  229.         l = (long)(PAGE_SIZE * PAGES);
  230.         Fread (fd, l, pad_ram);
  231.         Fclose (fd);
  232.     }
  233.     setbuf (stdout, NULL);
  234.     multi ();
  235.     }
  236. }
  237.  
  238.  
  239. /****************************************************************/
  240. /* dispatches all accessory tasks                */
  241. /****************************************************************/
  242. multi()
  243. {
  244.       int event;
  245.  
  246.       while (TRUE) {
  247.     event = evnt_multi (MU_MESAG | MU_BUTTON | MU_KEYBD,
  248.             1,1,1,
  249.             0,0,0,0,0,
  250.             0,0,0,0,0,
  251.             msgbuff, 0, 0, &mx, &my, &but, &ret, &keycode, &ret);
  252.  
  253.     wind_update (TRUE);
  254.     wind_get (wi_handle, WF_TOP, &top_window, &ret, &ret, &ret);
  255.  
  256.     if (event & MU_MESAG)
  257.       switch (msgbuff[0]) {
  258.  
  259.       case WM_REDRAW:
  260.         if (msgbuff[3] == wi_handle)
  261.           do_redraw (msgbuff[4],msgbuff[5],msgbuff[6],msgbuff[7]);
  262.         break;
  263.  
  264.       case WM_NEWTOP:
  265.       case WM_TOPPED:
  266.         if (msgbuff[3] == wi_handle)
  267.           wind_set(wi_handle,WF_TOP,0,0,0,0);
  268.         break;
  269.  
  270.       case AC_CLOSE:
  271.         if((msgbuff[3] == menu_id)&&(wi_handle != NO_WINDOW)){
  272.           v_clsvwk(handle);
  273.           wi_handle = NO_WINDOW;
  274.         }
  275.         break;
  276.  
  277.       case WM_CLOSED:
  278.         if(msgbuff[3] == wi_handle)
  279.             close_window ();
  280.         break;
  281.  
  282.       case WM_MOVED:
  283.         if (msgbuff[3] == wi_handle) {
  284.         if (msgbuff[6] < MIN_WIDTH) msgbuff[6] = MIN_WIDTH;
  285.         if (msgbuff[7] < MIN_HEIGHT) msgbuff[7] = MIN_HEIGHT;
  286.         wind_set (wi_handle,WF_CURRXYWH,msgbuff[4],msgbuff[5],msgbuff[6],msgbuff[7]);
  287.         wind_get (wi_handle, WF_WORKXYWH, &xwork, &ywork,&wwork,&hwork);
  288.         do_redraw (xwork, ywork, wwork, hwork);
  289.         }
  290.         break;
  291.  
  292.       case AC_OPEN:
  293.         if (msgbuff[4] == menu_id){
  294.           if(wi_handle == NO_WINDOW){
  295.         open_vwork();
  296.             open_window();
  297.           }
  298.           else    /* if already opened, for user convenience */
  299.             wind_set(wi_handle,WF_TOP,0,0,0,0);
  300.         }
  301.         break;
  302.  
  303. /*
  304.       case WM_FULLED:
  305.         if (fulled) {
  306.         wind_calc (WC_WORK,WI_KIND,xold,yold,wold,hold,
  307.                 &xwork,&ywork,&wwork,&hwork);
  308.         wind_set (wi_handle,WF_CURRXYWH,xold,yold,wold,hold);}
  309.         else{
  310.         wind_calc (WC_BORDER,WI_KIND,xwork,ywork,wwork,hwork,
  311.                 &xold,&yold,&wold,&hold);
  312.         wind_calc (WC_WORK,WI_KIND,xdesk,ydesk,wdesk,hdesk,
  313.                 &xwork,&ywork,&wwork,&hwork);
  314.         wind_set (wi_handle,WF_CURRXYWH,xdesk,ydesk,wdesk,hdesk);
  315.         }
  316.         fulled ^= TRUE;
  317.         break;
  318. */
  319.  
  320.       }    /* switch (msgbuff[0]) */
  321.  
  322.     if ((event & MU_BUTTON) && (wi_handle == top_window))
  323.       if (!but)
  324.       {
  325.         if (mx >= xwork && mx <= xwork + wwork)
  326.         {
  327.             if (my >= ywork && my <= ywork + hwork)
  328.             {
  329.                 scol[page] = ccol;
  330.                 sline[page] = cline;
  331.                 if (my < ywork + (hwork/2))
  332.                 {
  333.                     if (--page < 0)
  334.                         page = PAGES - 1;
  335.                 }
  336.                 else if (++page >= PAGES)
  337.                     page = 0;
  338.                 ccol = scol[page];
  339.                 cline = sline[page];
  340.                 do_redraw (xwork, ywork, wwork, hwork);
  341.             }
  342.         }
  343.       }
  344.  
  345.       if (event & MU_KEYBD) {
  346.          key_strike (keycode);
  347.       }
  348.  
  349.     wind_update (FALSE);
  350.  
  351.       } /* while (TRUE) */
  352.  
  353. }
  354.  
  355. /****************************************************************/
  356. /* Draw Filled Ellipse.                        */
  357. /****************************************************************/
  358.  
  359. draw_sample ()
  360. {
  361.     register int l;
  362. /*    int temp[4];    */
  363.  
  364.     set_window_name ();
  365. /*    vsf_interior (handle, 2);
  366.     vsf_style (handle, 8);
  367.     vsf_color (handle, 0);
  368.     *temp = xwork;
  369.     temp[1] = ywork;
  370.     temp[2] = xwork + wwork;
  371.     temp[3] = ywork + hwork;
  372.     v_bar (handle, temp);
  373.     vsf_color (handle, 1);
  374. */
  375.     for (l = 0; l < LINES; l++)
  376.         draw_line (l);
  377.     show_cursor ();
  378. }
  379.  
  380. draw_line (line)
  381. register int line;
  382. {
  383.     register char *p, *d;
  384.     register int i, c;
  385.  
  386.     p = pad_ram + (page*PAGE_SIZE)+(COLUMNS*line);
  387.     d = p + COLUMNS;
  388.     i = *d;
  389.     *d = '\0';
  390.     c = ywork + (cell_height * (line+1));
  391.     v_gtext (handle, xwork, c, p);
  392.     *d = i;
  393. }
  394.  
  395. key_strike (k)
  396. int k;
  397. {
  398.     register char *p, *d;
  399.     register int c, i;
  400.  
  401.     p = pad_ram + (page * PAGE_SIZE) + (cline * COLUMNS) + ccol;
  402.     c = k & 0xff;
  403.     erase_cursor (*p);
  404.     if (isprint (c))
  405.     {
  406.         *p = c;
  407.         erase_cursor (c);
  408.         ccol++;
  409.     } else if (c == '\r')
  410.     {
  411.         cline++;
  412.         ccol = 0;
  413.     } else if (c == '\t')
  414.         ccol += 4;
  415.     else if (c == '\b')
  416.     {
  417.         erase_cursor (*p--);
  418.         *p = ' ';
  419.         ccol--;
  420.     } else if (c == 0x7f)
  421.     {
  422.         for (c=1; c < COLUMNS-ccol; c++)
  423.         {
  424.             *p = p[1];
  425.             p++;
  426.         }
  427.         *p = ' ';
  428.         draw_line (cline);
  429.     } else if (c == 0)
  430.     {
  431.         c = (k >> 8) & 0xff;
  432.         switch (c) {
  433.             case 60:        /* F2 Delete Line */
  434.                 p = pad_ram+(page*PAGE_SIZE)+(cline*COLUMNS);
  435.                 d = p + COLUMNS;
  436.                 for (c=cline+1; c<LINES; c++)
  437.                     for (i=0; i<COLUMNS; i++)
  438.                         *p++ = *d++;
  439.                 for (i=0; i<COLUMNS; i++)
  440.                     *p++ = ' ';
  441.                 draw_sample ();
  442.                 break;
  443.             case 68:        /* F10 Insert Line */
  444.                 p = pad_ram+((page+1)*PAGE_SIZE)-1;
  445.                 d = p-COLUMNS;
  446.                 for (c=cline+1; c<LINES; c++)
  447.                     for (i=0; i<COLUMNS; i++)
  448.                         *p-- = *d--;
  449.                 d++;
  450.                 for (i=0; i<COLUMNS; i++)
  451.                     *d++ = ' ';
  452.                 draw_sample ();
  453.                 break;
  454.             case 0x48:
  455.                  cline--;
  456.                 break;
  457.             case 0x4b:
  458.                 ccol--;
  459.                 break;
  460.             case 0x50:
  461.                 cline++;
  462.                 break;
  463.             case 0x4d:
  464.                 ccol++;
  465.                 break;
  466.             case 82:        /* Insert key */
  467.                 p =pad_ram+(page*PAGE_SIZE)+((cline+1)*COLUMNS);
  468.                 p--;
  469.                 for (c=1; c < COLUMNS-ccol; c++)
  470.                 {
  471.                     p--;
  472.                     p[1] = *p;
  473.                 }
  474.                 *p = ' ';
  475.                 draw_line (cline);
  476.                 break;
  477.             case 59:        /*  F1    clear page */
  478.                 p = pad_ram + (page * PAGE_SIZE);
  479.                 for (c=0; c<PAGE_SIZE; c++)
  480.                     *p++ = ' ';
  481.                 ccol = cline = 0;
  482.                 draw_sample ();
  483.                 break;
  484.             case 97:        /* UNDO key */
  485.                 save_pad ();
  486.                 close_window ();
  487.                 break;
  488.         }
  489.     }
  490.     if (ccol < 0)
  491.     {
  492.         ccol = COLUMNS -1;
  493.         cline--;
  494.     }
  495.     else if (ccol >= COLUMNS)
  496.     {
  497.         ccol = 0;
  498.         cline++;
  499.     }
  500.     if (cline < 0)
  501.         cline++;
  502.     if (cline >= LINES)
  503.         cline--;
  504.     show_cursor ();
  505. }
  506.  
  507. show_cursor ()
  508. {
  509.     register char *p;
  510.  
  511.     p = pad;
  512.     *p = '_';
  513.     p[1] = '\0';
  514.     v_gtext (handle, xwork + (cell_width * ccol),
  515.             ywork + (cell_height * (1 + cline)), p);
  516. }
  517.  
  518. erase_cursor (c)
  519. int c;
  520. {
  521.     register char *p;
  522.  
  523.     p = pad;
  524.     *p = c;
  525.     p[1] = '\0';
  526.     v_gtext (handle, xwork + (cell_width * ccol),
  527.                 ywork + (cell_height * (cline + 1)), p);
  528. }
  529.  
  530. close_window ()
  531. {
  532.     wind_close (wi_handle);
  533.     graf_shrinkbox (xwork+wwork/2,ywork+hwork/2,gl_wbox,gl_hbox,xwork,ywork,wwork,hwork);
  534.     wind_delete (wi_handle);
  535.     v_clsvwk (handle);
  536.     wi_handle = NO_WINDOW;
  537. }
  538.  
  539. set_window_name ()
  540. {
  541.     strcpy (pad, " The Pad   ");
  542.     pad[9] = page + 0x31;
  543.     wind_set (wi_handle, WF_NAME, pad, 0, 0);
  544. }
  545.  
  546. save_pad ()
  547. {
  548.     register char    *p;
  549.     register int    fd;
  550.     register long    l;
  551.  
  552.     fd = Fopen (file, 1);    
  553.     if (fd >= 0)
  554.     {
  555.         l = (long)(PAGE_SIZE * PAGES);
  556.         Fwrite (fd, l, pad_ram);
  557.         Fclose (fd);
  558.     }
  559. }
  560.